/*
 * @(#)PostalAddressTransformer.java
 * Date 2013-08-16
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.pix.transformer;

import java.io.Serializable;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import javax.xml.bind.JAXBElement;

import org.apache.commons.lang3.StringUtils;

import org.hl7.v3.AD;
import org.hl7.v3.ADXP;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.hornerscorners.lamppost.config.ModelConfig;

import us.hornerscorners.lamppost.hl7v3common.utils.HL7Utils;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.model.common.CodedElement;

import us.hornerscorners.lamppost.model.patient.PostalAddress;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class PostalAddressTransformer extends AbstractTransformer<AD, PostalAddress> {

    /** Field description */
    @Inject
    private IHEConfig iheConfig;

    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * Method description
     *
     *
     * @param prpaAddress
     *
     * @return
     */
    @Override
    public PostalAddress transform(AD prpaAddress) {

        PostalAddress result = null;

        if (prpaAddress != null) {

            result = new PostalAddress();

            String usestr = HL7Utils.parseFirstString(prpaAddress.getUse());

            if (StringUtils.isNotBlank(usestr)) {

                ModelConfig modelConfig = this.iheConfig.getModelConfig();

                CodedElement elem = new CodedElement();

                elem.setCode(usestr);
                elem.setCodeSystem(modelConfig.getAddressUseCodeSystem());

                result.setPurpose(elem);
            }

            for (Serializable serializable : prpaAddress.getContent()) {

                if (serializable instanceof JAXBElement) {

                    JAXBElement jaxbElement = (JAXBElement) serializable;
                    String value = HL7Utils.parseFirstSerializable(
                                       ((ADXP) jaxbElement.getValue()).getContent());

                    String tagName = jaxbElement.getName().getLocalPart();

                    switch (tagName) {

                        case "city" :
                            result.setCity(value);

                            break;

                        case "country" :
                            result.setCountry(value);

                            break;

                        case "county" :
                            result.setCounty(value);

                            break;

                        case "state" :
                            result.setState(value);

                            break;

                        case "streetAddressLine" :
                            result.getStreetLines().add(value);

                            break;

                        case "postalCode" :
                            result.setZipCode(value);

                            break;

                        default :
                            logger.warn("Unhandled address element {}.", tagName);

                            break;
                    }
                }
            }
        }

        return result;
    }
}
